package kr.co.solosoft.nextdayjob.data;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class JobXmlParser
{
	private ArrayList<ResultData> resultArray;
	private ResultData data;
	private String tag;
	
	public JobXmlParser(String xml)
	{
		try
		{
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			
			XmlPullParser xpp = factory.newPullParser();
			xpp.setInput(new StringReader(xml));
			
			int eventType = xpp.getEventType();
			
			while(eventType != XmlPullParser.END_DOCUMENT)
			{
				if(eventType == XmlPullParser.START_DOCUMENT) 
				{
					//시작
					resultArray = new ArrayList<ResultData>();
				} 
				else if(eventType == XmlPullParser.END_TAG) 
				{
					tag = xpp.getName();
					
					if(tag.equals("item"))
					{
					
						resultArray.add(data);
					}
		        } 
				else if(eventType == XmlPullParser.TEXT) 
				{
			
		        }
				else if(eventType == XmlPullParser.START_TAG) 
				{
					//시작태그
					tag = xpp.getName();
				
					if(tag.equals("item"))
					{
						data = new ResultData(); 
					}
					else if(tag.equals("Job_Title"))
					{
						data.setJobTitle(xpp.nextText());
					}
					else if(tag.equals("Employ_Ty_Nm"))
					{
						data.setEmployType(xpp.nextText());
					}
					else if(tag.equals("Comp_Nm"))
					{
						data.setCompanyName(xpp.nextText());
					}
					else if(tag.equals("Region_Nm"))
					{
						data.setRegionName(xpp.nextText());
					}
					else if(tag.equals("Region_Det_Nm"))
					{
						data.setDetailRegionName(xpp.nextText());
					}
					else if(tag.equals("Subway_Str"))
					{
						data.setSubway(xpp.nextText());
					}
					else if(tag.equals("Schol_Nm"))
					{
						data.setScholName(xpp.nextText());
					}
					else if(tag.equals("Career_Nm"))
					{
						data.setCareerName(xpp.nextText());
					}
					else if(tag.equals("Salary_Nm"))
					{
						data.setSalaryName(xpp.nextText());
					}
					else if(tag.equals("Job_Cont"))
					{
						data.setJonContent(xpp.nextText());
					}
					else if(tag.equals("Cl_Invite_Start_Dt"))
					{
						data.setStartDate(xpp.nextText());
					}
					else if(tag.equals("Cl_Invite_Close_Dt"))
					{
						data.setEndDate(xpp.nextText());
					}
					else if(tag.equals("Employ_limit"))
					{
						data.setEmployLimit(xpp.nextText());
					}
					else if(tag.equals("Source_Site_Nm"))
					{
						data.setSiteName(xpp.nextText());
					}
					else if(tag.equals("Source_Site_Url"))
					{
						data.setSiteUrl(xpp.nextText());
					}
					else if(tag.equals("Link_Url"))
					{
						data.setLinkUrl(xpp.nextText());
					}
					else if(tag.equals("Cl_Date"))
					{
						data.setClDate(xpp.nextText());
					}
		        } 
				eventType = xpp.next();
			}
		} 
		catch (XmlPullParserException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e) 
		{
			// TODO: handle exception
		}
	}

	public ArrayList<ResultData> getResultArray()
	{
		return resultArray;
	}
}
